/*
 * 文件名：BpaIncaeSureServiceImpl.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年4月16日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.service.impl;


import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import top.infopub.mgr.bpa.dao.IncaeSureDao;
import top.infopub.mgr.bpa.dao.IncaeSureDetailDao;
import top.infopub.mgr.bpa.dao.OutcaeSureDao;
import top.infopub.mgr.bpa.domain.IncaeSureDetailAndSubDomain;
import top.infopub.mgr.bpa.service.BpaIncaeSureService;
import top.infopub.model.bpa.IncaeSure;

import com.google.common.collect.Maps;

@Service
public class BpaIncaeSureServiceImpl implements BpaIncaeSureService {

    @Autowired
    private IncaeSureDao incaeSureDao;

    @Autowired
    private IncaeSureDetailDao incaeSureDetailDao;

    @Autowired
    private OutcaeSureDao outcaeSureDao;

    @Override
    public void bpaIncaeSure(String proCode) {
        Map<String, Object> map = Maps.newHashMap();
        List<IncaeSureDetailAndSubDomain> subDomains = incaeSureDetailDao.queryByProCode(proCode);
        for (IncaeSureDetailAndSubDomain iAndSubDomain : subDomains) {
            //操作detail明细表
            map.put("subName", iAndSubDomain.getSubName());
            map.put("refId", iAndSubDomain.getRefIdBpa());
            map.put("typeName", iAndSubDomain.getTypeName());
            map.put("sureMonthBpa", iAndSubDomain.getSureMonthBpa());
            BigDecimal addupAmountAfter = incaeSureDetailDao.selectAddupAmountAfter(map);
            if (null == addupAmountAfter) {
                iAndSubDomain.setAddupAfterAmount(BigDecimal.ZERO);
            }
            iAndSubDomain.setAddupAfterAmount(addupAmountAfter);
            incaeSureDetailDao.updateByIdSelective(iAndSubDomain);
            //操作主表
            IncaeSure incaeSure = incaeSureDao.selectByPrimaryKey(iAndSubDomain.getIdBpa());
            //为开累对内确权金额（下浮后）合计
            BigDecimal afterAmountAfter = incaeSureDao.selectMonthAfterAmountAfter(
                iAndSubDomain.getSureMonthBpa(), proCode);
            if (null == afterAmountAfter) {
                incaeSure.setAddupAfterAmount(BigDecimal.ZERO);
            }
            incaeSure.setAddupAfterAmount(afterAmountAfter);
            //开累对外确权金额（下浮后）（元）
            Map<String, Object> whereMap = new HashMap<String, Object>();
            whereMap.put("proCode", proCode);
            whereMap.put("sureMonth", iAndSubDomain.getSureMonthBpa());
            BigDecimal decimalByProCode = outcaeSureDao.selectDecimalByProCode(whereMap);
            if (null == decimalByProCode) {
                incaeSure.setAddupOutAmount(BigDecimal.ZERO);
            }
            incaeSure.setAddupOutAmount(decimalByProCode);
            incaeSureDao.updateByIdSelective(incaeSure);
        }

    }

}
